<?php

namespace App\Http\Controllers\user;

use App\Dao\BillDao;
use App\Dao\TaskDao;
use App\Dao\UsersDao;
use App\Exceptions\SysExceptions;
use App\Services\Code;
use App\Services\OssServices;
use App\Services\WechatServices;
use EasyWeChat\Kernel\Exceptions\HttpException;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use EasyWeChat\Kernel\Exceptions\RuntimeException;

/**
 * Created by PhpStorm.
 * @purpose
 * @Author: fhc
 * @Time: 2023/5/11 16:24
 */
class UsersController extends BaseController
{



    /**
     * Created by PhpStorm.
     * @purpose 用户登陆
     * @Author: fhc
     * @Time: 2023/5/11 16:25
     */
    public function login(): array
    {
        $params = request()->all();
//        WechatServices::wechat_login('','','');
        return Code::message('',200,UsersDao::login($params));
    }


    /**
     * Created by PhpStorm.
     * @purpose 用户信息
     * @Author: fhc
     * @Time: 2023/5/11 16:28
     */
    public function user_info(): array
    {
        $params = [
            'user_id'=>getUserToken()
        ];
        return Code::message('',200,UsersDao::getUserInfo($params));
    }


    /**
     * Created by PhpStorm.
     * @purpose 自动安放节点
     * @Author: fhc
     * @Time: 2023/5/12 15:26
     */
    public function setAutoPoint(): array
    {
        UsersDao::setAutoPoint([
            'user_id'=>getUserToken(),
            'is_auto'=>request('is_auto')
        ]);
        return Code::message('',200);
    }


    /**
     * Created by PhpStorm.
     * @purpose 设置节点
     * @Author: fhc
     * @Time: 2023/5/12 18:16
     * @throws SysExceptions
     */
    public function set_point(): array
    {
        $params = array_merge(request()->all(),[
            'user_id'=>getUserToken()
        ]);
        UsersDao::set_point_uid($params);
        return Code::message('',200);
    }


    /**
     * Created by PhpStorm.
     * @purpose 获取账单
     * @Author: fhc
     * @Time: 2023/5/14 12:42
     */
    public function get_bills(): array
    {
        $all = array_merge(request()->all(),[
            'user_id'=>getUserToken()
        ]);
        return Code::message('',200,BillDao::get_bills($all));
    }


    /**
     * Created by PhpStorm.
     * @purpose 获取好友列表
     * @Author: fhc
     * @Time: 2023/5/14 22:32
     */
    public function get_users_spread(): array
    {
        return Code::message('',200,UsersDao::get_users_spread(request()->all()));
    }


    public function get_child_users_point(): array
    {
        return Code::message('',200,UsersDao::get_child_users_point([
            'user_id'=>request('user_id')
        ]));
    }



    public function userQrCode(): array
    {
        return Code::message('',200,UsersDao::usr_qr_code([
            'user_id'=>getUserToken()
        ]));
    }



    public function update_nickname_headimgurl(): array
    {
        $params = array_merge(request()->all(),[
            'user_id'=>getUserToken()
        ]);
        UsersDao::update_nickname_headimgurl($params);
        return Code::message('',200);
    }

     public function file_up(): array
     {
        $filePath = $_FILES["file"]["tmp_name"];
        $ext = substr($_FILES['file']['name'],strrpos($_FILES['file']['name'],'.')+1); // 上传文件后缀
        $object=date('YmdHis').'-'.time().'.'.$ext;
        $oss = new OssServices();
        $url = $oss->upload($object,$filePath);
        return Code::message('',200,[
            'url'=>$url
        ]);

    }


    /**
     * @throws SysExceptions
     */
    public function update_user_name(): array
    {
        $params = array_merge([
            'user_id'=>getUserToken()
        ],request()->all());
        UsersDao::update_user_name($params);
        return Code::message('',200);
    }



    public function get_user_task_log(): array
    {
        return Code::message('',200,TaskDao::get_task_log([
            'user_id'=>getUserToken()
        ]));
    }



    public function get_data_count(): array
    {
        return Code::message('',200,UsersDao::get_data_count([
            'user_id'=>getUserToken()
        ]));
    }


    public function set_card_json(): array
    {
        UsersDao::set_card_json(array_merge([
            'user_id'=>getUserToken(),
        ],request()->all()));
        return Code::message('',200);
    }


    /**
     * @throws SysExceptions
     */
    public function widthwal(): array
    {
        $params = array_merge(request()->all(),[
            'user_id'=>getUserToken()
        ]);
        UsersDao::widthwal($params);
        return Code::message();
    }



    public function get_user_chengyuans(): array
    {
        return Code::message('',200,UsersDao::get_zongjian_usrs([
            'user_id'=>getUserToken()
        ]));
    }


    /**
     * @return array
     * @throws SysExceptions
     * @throws HttpException
     * @throws InvalidArgumentException
     * @throws InvalidConfigException
     * @throws RuntimeException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function update_user_phone_bind(): array
    {
        $params = request()->all();
        UsersDao::user_bind_phone($params);
        return Code::message('',200);
    }


}
